package com.bawei.module_mine.ui

import android.content.Context
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.PopupWindow
import androidx.core.view.contains

import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.Router
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.module_mine.R
import com.bawei.module_mine.databinding.ActivityPhoneLoginBinding
import com.bawei.module_mine.model.GetPermissions
import com.blankj.utilcode.util.ToastUtils

@Route(path = Router.Ui.PHONE_LOGIN_ACTIVITY)
class PhoneLoginActivity : BaseActivity() {
    private val binding by lazy {
        ActivityPhoneLoginBinding.inflate(layoutInflater)
    }
    private lateinit var popupWindow:PopupWindow

    private var dimView : View? = null
    override fun initView() {
        super.initView()
        initPopupWindow()
        binding.ivMore.setOnClickListener {
            showPopupWithDim()
        }
        if (GetPermissions.regexPhoneNumber(binding.btnGetPhoneCode.text.toString())){
            binding.btnGetPhoneCode.setBackgroundColor(R.drawable.shape_phone_login_code1)
        }
        binding.ivBack.setOnClickListener {
            finish()
        }
        binding.tvHelp.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.HELP_ACTIVITY).navigation()
        }
        val spannable = SpannableStringBuilder(binding.tvProtocol.text.toString())
        val spannable1 = SpannableStringBuilder(binding.tvPrivacy.text.toString())
        spannable.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable1.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(Router.Ui.WEB_ACTIVITY)
                    .withString("url","https://terms.alicdn.com/legal-agreement/terms/suit_bu1_unification/suit_bu1_unification202005142208_14749.html")
                    .navigation()
            }

        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvProtocol.setText(spannable)
        binding.tvProtocol.setMovementMethod(LinkMovementMethod.getInstance())
        spannable1.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(Router.Ui.WEB_ACTIVITY)
                    .withString("url","https://www.douyin.com/draft/douyin_agreement/douyin_agreement_privacy.html?id=6773901168964798477")
                    .navigation()
            }

        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvPrivacy.text = spannable1
        binding.tvPrivacy.movementMethod = LinkMovementMethod.getInstance()
        binding.btnGetPhoneCode.setOnClickListener {
            if (GetPermissions.regexPhoneNumber(binding.etPhoneLogin.text.toString())){
                ARouter.getInstance().build(Router.Ui.CODE_ACTIVITY).navigation()
            }else{
                ToastUtils.showLong("手机号格式错误，请重新输入")
            }
        }
        binding.llPhoneCodeEt.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.LOGIN_Actvivty).navigation()
        }
        dimView?.setOnClickListener {
            popupWindow.dismiss()
            binding.main.removeView(dimView)
        }
        popupWindow.setOnDismissListener {
            if (binding.main.contains(dimView!!)) {
                binding.main.removeView(dimView)
            }
        }
    }

    override fun initData() {
        super.initData()
        binding.etPhoneLogin.setOnClickListener {
            binding.etPhoneLogin.requestFocus()
            showKeyBoard(binding.etPhoneLogin)
        }
    }

    private fun showKeyBoard(view:View){
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }

    private fun showPopupWithDim() {
        // 1. 创建半透明蒙层 View
        dimView = View(this).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            // 设置半透明背景，这里用 rgba 值，最后一位 0.5 表示透明度（可根据需求调整）
            setBackgroundColor(Color.parseColor("#80000000"))
            // 设置点击事件，点击蒙层不做特殊处理（也可在此处理关闭弹窗逻辑）
            setOnClickListener { /* 可空实现，或根据需求关闭弹窗 */ }
        }
        // 2. 将蒙层添加到主布局
        binding.main.addView(dimView)


        // 3. 显示 PopupWindow（这里假设你之前是用 showAsDropDown 或 showAtLocation，按需调整 ）
        popupWindow.showAtLocation(
            binding.root,
            Gravity.BOTTOM,
            0,
            0
        )
    }
    private fun initPopupWindow(){
        val popupView = layoutInflater.inflate(R.layout.popup_login,null)
        popupWindow = PopupWindow(
            popupView,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
        )
        popupView.findViewById<View>(R.id.tvCancel).setOnClickListener {
            popupWindow.dismiss()
            binding.main.removeView(dimView)
        }

    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

}